package com.sky.employee.controller.login;


import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.dto.PasswordEditDTO;
import com.sky.employee.service.EmployeeService;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.Map;

/**
 * 后台员工管理
 */
@Slf4j
//           分组            描述
@Api(tags = {"后台员工管理"}, description = "后台员工操作")
@Validated //非空校验形参接收的变量
@RestController
@RequestMapping("/admin/employee")
public class EmployeeController {


    @Resource
    private JwtProperties jwtProperties;

    @Resource
    private EmployeeService employeeService;




    /**
     * 员工登录
     *       入参username、password
     */
    //            可见摘要            注释                  分组
    @ApiOperation(value = "员工登录", notes = "员工登录接口", tags = {"后台员工管理"})
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody @Validated EmployeeLoginDTO employeeLoginDTO){
        //System.out.println("当前线程id：" + Thread.currentThread().getId());
        log.info("后台员工登录：{}", employeeLoginDTO);
        Employee employee = employeeService.login(employeeLoginDTO);

        // 生成令牌token
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String jwtToken = JwtUtil.createJWT(jwtProperties.getAdminSecretKey(), jwtProperties.getAdminTtl(), claims);

        // 封装返回值对象
        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token("Bearer " + jwtToken)
                .build();
        return Result.success(employeeLoginVO);
    }



    /**
     * 退出
     */
    @ApiOperation(tags = {"后台员工管理"}, value = "员工登出", notes = "登出系统")
    @PostMapping("/logout")
    public Result<String> logout() {
        System.out.println("退出系统成功");
        return Result.success();
    }



    /**
     * 员工管理
     *         新增员工
     * @param employeeDTO
     */
    @ApiOperation(tags = {"后台员工管理"}, value = "添加员工", notes = "数据库查询新员工数据")
    @PostMapping
    public Result addEmployee(@RequestBody @Validated EmployeeDTO employeeDTO){
        //System.out.println("线程id：" + Thread.currentThread().getId());
        log.info("员工管理 - 新增员工数据：{}", employeeDTO);
        employeeService.saveEmployee(employeeDTO);
        return Result.success();
    }



    /**
     * 员工管理
     *         分页-条件查询
     */
    @ApiOperation(tags = {"后台员工管理"}, value = "员工分页条件查询" , notes = "分页条件查询员工列表")
    @GetMapping("/page")
    public Result<PageResult> page(EmployeePageQueryDTO employeePageQueryDTO){
        log.info("员工管理 - 员工分页条件查询：{}", employeePageQueryDTO);
        PageResult pageResult = employeeService.pageQuery(employeePageQueryDTO);
        return Result.success(pageResult);
    }



    /**
     * 员工管理
     *         员工启用、禁用 修改
     * @param id
     */
    @ApiOperation(tags = {"后台员工管理"}, value = "员工启用、禁用", notes = "员工启用、禁用修改")
    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable(name="status")Integer status, @NotNull(message="id为空")Long id){
        log.info("员工管理 - 员工id：{}， status(0禁用/1启用) ==> {}", id, status);
        employeeService.startOrStop(id, status);
        return Result.success();
    }



    /**
     * 员工管理
     *        根据id查询
     * @param id
     */
    @ApiOperation(tags = {"后台员工管理"}, value = "根据员工id查询", notes = "根据员工id查询")
    @GetMapping("/{id}")
    public Result<Employee> selectEmpById(@PathVariable(name="id")Long id){
        log.info("员工管理 - 根据id查询；{}", id);
        Employee employee = employeeService.getById(id);
        return Result.success(employee);
    }



    /**
     * 员工管理
     *        根据id修改
     * @param employeeDTO
     */
    @ApiOperation(tags = {"后台员工管理"}, value = "根据员工id修改", notes = "根据员工id修改员工信息")
    @PutMapping
    public Result updateById(@RequestBody @Validated EmployeeDTO employeeDTO){
        log.info("员工管理 - 编辑员工信息：{}", employeeDTO);
        employeeService.updateById(employeeDTO);
        return Result.success();
    }



    /**
     * 员工管理
     *         修改密码
     */
    @PutMapping("/editPassword")
    public Result editPassword(@RequestBody @Validated PasswordEditDTO passwordEditDTO){
        log.info("员工管理 - 修改密码：{}", passwordEditDTO);
        employeeService.updatePasswordById(passwordEditDTO);
        return Result.success();
    }


}
